<template>
  <div>
    <BasicTable @register="registerTable" :searchInfo="searchInfo">
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <UserGoodsSendMessageDrawer @register="registerUserGoodsSendMessageDrawer" />
  </div>
</template>
<script lang="ts" setup>
import { ActionItem, BasicTable, TableAction } from '@apps/antd-admin/src/components/table';
import { useBaseTable } from '@apps/antd-admin/src/hooks';
import { columns, searchFormSchema } from '../data/UserGoodsData';
import { changeRefundState, UserGoodsApi } from '../data/UserGoodsApi';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';
import UserGoodsSendMessageDrawer from './UserGoodsSendMessageDrawer.vue';
import { reactive } from 'vue';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';

const { createMessage, createConfirm } = useMessage();

const [registerUserGoodsSendMessageDrawer, { openDrawer: openUserGoodsSendMessageDrawer }] = useDrawer();
const { prefixCls, tableContext, handleDelete, handleSuccess, handlePost, handleBatchDelete } = useBaseTable({
  designScope: 'userGoodsList-template',
  tableProps: {
    //表格标题
    title: '券码列表',
    //主键字段
    rowKey: 'userGoodsId',
    //列表查询handler
    handlerName: 'generateInfoAppUserGoodsListHandler',
    //列表接口
    listUrl: UserGoodsApi.page,
    //列定义
    columns,
    //查询表单配置
    formConfig: {
      labelWidth: 150,
      labelCol: { span: 5 },
      schemas: searchFormSchema,
      showExportButton: true,
      exportButtonOptions: {
        code: 'ACTION_userGoodsQrcodeExport',
      },
      isCheckExportTime: false,
    },
    //操作列配置
    actionColumn: {
      width: 160,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
  },
});

const [registerTable, { reload, setProps }, { rowSelection, hasSelectedRows }] = tableContext;

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '标记退款',
      onClick: handleTagRefund.bind(null, record),
      ifShow: record.refundState == 0,
    },
    {
      label: '发送信息',
      onClick: handleSendMsg.bind(null, record),
    },
  ];
}

function handleTagRefund(record: Recordable) {
  let qrCode = record.qrCode;
  let userGoodsId = record.userGoodsId;
  createConfirm({
    iconType: 'info',
    title: '是否标记退款',
    content: '确认券号为' + qrCode + '的商品标记退款？',
    onOk: async () => {
      await changeRefundState({ userGoodsId: userGoodsId });
      reload();
    },
    onCancel: async () => {},
  });
}

function handleSendMsg(record: Recordable) {
  openUserGoodsSendMessageDrawer(true, {
    record,
    isUpdate: true,
  });
}

const searchInfo = reactive<Recordable>({});

function setSearchInfoProps(businessId) {
  searchInfo.businessId = businessId;
}

defineExpose({
  setSearchInfoProps,
});
</script>
